﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace StreamingClient.StreamManagment
{
    public class SettingsVM: INotifyPropertyChanged
    {
        private StreamingSettings _settings;
        public StreamingSettings Settings
        {
            get { return _settings; }
            set { _settings = value;  Notify("Settings");}
        }


        private List<ServerType> _types;
        public List<ServerType> Types
        {
            get
            {
                if (_types == null)
                {
                    _types = new List<ServerType>() { ServerType.VLC, ServerType.WMV3 };
                }
                return _types;
            }
        }
        private List<PresetTypes> _presets;

        public SettingsVM(StreamingSettings streamingSettings)
        {
            Settings = streamingSettings;
        }


        public List<PresetTypes> Presets
        {
            get
            {
                if (_presets == null)
                {
                    _presets = new List<PresetTypes>() { PresetTypes.Low, PresetTypes.Medium, PresetTypes.High, PresetTypes.Custom};
                }
                return _presets;
            }
        }

        public PresetTypes Preset
        {
            get { return Settings.PresetType; }
            set
            {
                switch (value)
                {
                    case PresetTypes.Low:
                        Settings.PresetLow();
                        break;
                    case PresetTypes.Medium:
                        Settings.PresetMedium();
                        break;
                    case PresetTypes.High:
                        Settings.PresetHigh();
                        break;
                    case PresetTypes.Custom:
                        Settings.PresetType=PresetTypes.Custom;
                        break;
                }

                Notify("Preset");
                Notify("Settings");
                Notify("ShowAdvanced");
            }
        }       
        
        public ServerType ServerType
        {
            get { return Settings.ServerType; }
            set
            {
                Settings.ServerType = value;
                switch (value)
                {
                    case ServerType.WMV3:
                        Settings.ControlPort = 8011;
                        break;
                    case ServerType.VLC:
                        Settings.ControlPort = 8080;
                        break;
                }
                Notify("ServerType");
                Notify("Settings");
                Notify("ShowWMV");
                Notify("ShowVLC");
            }
        }

        public Visibility ShowAdvanced { get { return Preset == PresetTypes.Custom?Visibility.Visible : Visibility.Collapsed; } }
        public Visibility ShowWMV { get { return ServerType == ServerType.WMV3 ? Visibility.Visible : Visibility.Collapsed; } }
        public Visibility ShowVLC { get { return ServerType == ServerType.VLC ? Visibility.Visible : Visibility.Collapsed; } }
        public bool EnableAdvanced { get { return Preset == PresetTypes.Custom; } }
        public bool EnableWMV { get { return ServerType == ServerType.WMV3; } }


        
        public void Save()
        {
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void Notify(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
}
